package com.sc.controller.rui;

import com.sc.domain.Hire;
import com.sc.dto.HireDTO;
import com.sc.result.Result;
import com.sc.service.HireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/hire")
public class HireController {

    @Autowired
    private HireService hireService;

    @PostMapping("/add")
    public Result add(@RequestBody Hire hire) {
        try {
            return Result.success("添加成功", hireService.add(hire));
        } catch (DuplicateKeyException e) {
            return Result.error("房屋信息重复，换一个试试");
        }
    }

    @PostMapping("/pageQuery")
    public Result pageQuery(@RequestBody HireDTO hireDTO) {
        return Result.success(hireService.pageQuery(hireDTO));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Hire hire) {
        return Result.success("修改成功", hireService.update(hire));
    }

    @GetMapping("/selectById")
    public Result<Hire> selectById(Integer id) {
        return Result.success(hireService.selectById(id));
    }

    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id) {
        return Result.success("删除成功", hireService.deleteById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", hireService.deleteByIds(ids));
    }
}
